/*******************************************************************************
* @copyright: Shenzhen Hangshun Chip Technology R&D Co., Ltd
* @filename:  main.c
* @brief:     Main program body
* @author:    AE Team
* @version:   V1.0.0/2025-07-16
*             1.Initial version
* @log:
*******************************************************************************/


/* Includes ------------------------------------------------------------------*/
#include "main.h"


/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
I2C_DEVICE_TypeDef MyI2C_Device;

/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/
static void I2C_Configuration(void);
static void UART_Config(void);
static void Delay(__IO uint32_t nCount);

/**
  * @brief  Main program.
  * @param  None
  * @retval None
  */
int main(void)
{
  uint8_t i;

  /*!< At this stage the microcontroller clock setting is already configured,
       this is done through SystemInit() function which is called from startup
       file (KEIL_startup_hk32g003.s) before to branch to application main.
       To reconfigure the default setting of SystemInit() function, refer to
       system_hk32g003.c file
     */

  /* Inser some delay time for easy debug purpose  */
  Delay(0xFFFFF);

  /* UART configuration */
  UART_Config();

  printf("HK32F0301MG6P7A I2C Slave Example!\r\n");

  I2C_Configuration();

  for( i = 0; i < I2C_REG_SIZE; i ++ )
  {
    MyI2C_Device.DataReg[i] = 0x0;
  }

  MyI2C_Device.DataRegAddress = 0;
  MyI2C_Device.ErrorStatus = 0;
  MyI2C_Device.EventStatus = 0;
  MyI2C_Device.FirstWriteByteFlag = RESET;
  MyI2C_Device.ReadRegCount = 0;
  MyI2C_Device.ReadRegLen = 0;
  MyI2C_Device.WriteRegCount = 0;
  MyI2C_Device.WriteRegLen = 0;
  MyI2C_Device.RegTemp = 0;

  /* Infinite loop */
  while (1)
  {
    if( MyI2C_Device.ReadRegLen > 0 )
    {
      printf("\r\n   Send:");

      for( i = 0; i < MyI2C_Device.ReadRegLen; i++ )
      {
        printf("0x%02X ", MyI2C_Device.DataReg[MyI2C_Device.DataRegAddress + i]);
      }

      MyI2C_Device.ReadRegLen = 0;
    }

    if( MyI2C_Device.WriteRegLen > 0)
    {
      printf("\r\nReceive:");

      for( i = 0; i < MyI2C_Device.WriteRegLen; i++ )
      {
        printf("0x%02X ", MyI2C_Device.DataReg[MyI2C_Device.DataRegAddress + i]);
      }

      MyI2C_Device.WriteRegLen = 0;
    }
  }
}

/**
  * @brief  Configure the I2C
  * @param  None
  * @retval None
  */
void I2C_Configuration(void)
{
  I2C_InitTypeDef		I2C_InitStructure;
  GPIO_InitTypeDef	GPIO_InitStructure;
  NVIC_InitTypeDef NVIC_InitStructure;

  /* Enable GPIO Clock */
  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOC, ENABLE);

  /* Enable I2C Clock */
  RCC_APBPeriph1ClockCmd(RCC_APBPeriph1_I2C, ENABLE);

  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_6;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  GPIO_InitStructure.GPIO_PuPd =  GPIO_PuPd_UP;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;
  GPIO_Init(GPIOC, &GPIO_InitStructure);

  GPIO_PinAFConfig(GPIOC, GPIO_PinSource5, GPIO_AF_0);
  GPIO_PinAFConfig(GPIOC, GPIO_PinSource6, GPIO_AF_0);

  NVIC_InitStructure.NVIC_IRQChannel = I2C_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPriority = 1;
  NVIC_InitStructure.NVIC_IRQChannelCmd  = ENABLE;
  NVIC_Init(&NVIC_InitStructure);

  RCC_I2CCLKConfig(RCC_I2C1CLK_SYSCLK);

  /* Enable I2C TXI,ERRI,TCI,STOPI,NACKI,ADDRI,RXI interrupt */
  I2C_ITConfig(I2C, I2C_IT_TXI | I2C_IT_ERRI | I2C_IT_TCI | I2C_IT_STOPI | I2C_IT_NACKI | I2C_IT_ADDRI | I2C_IT_RXI, ENABLE);

  /* Configure I2C */
  I2C_InitStructure.I2C_Timing = 0x2000090E;   /* About 100kH */
  I2C_InitStructure.I2C_AnalogFilter = I2C_AnalogFilter_Enable;
  I2C_InitStructure.I2C_DigitalFilter = 0;
  I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;
  I2C_InitStructure.I2C_OwnAddress1 = I2C_SLAVE_ADDRESS;
  I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;
  I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;

  /* Initilze I2C parameter */
  I2C_Init(I2C, &I2C_InitStructure);

  /* Enable I2C */
  I2C_Cmd(I2C, ENABLE);
}

/**
  * @brief  Configure the UART Device
  * @param  None
  * @retval None
  */
static void UART_Config(void)
{
  UART_InitTypeDef UART_InitStructure;

  /* UARTx configured as follow:
  - BaudRate = 115200 baud
  - Word Length = 8 Bits
  - Stop Bit = 1 Stop Bit
  - Parity = No Parity
  - Hardware flow control disabled (RTS and CTS signals)
  - Receive and transmit enabled
  */
  UART_InitStructure.UART_BaudRate = 115200;
  UART_InitStructure.UART_WordLength = UART_WordLength_8b;
  UART_InitStructure.UART_StopBits = UART_StopBits_1;
  UART_InitStructure.UART_Parity = UART_Parity_No;
  UART_InitStructure.UART_Mode = UART_Mode_Rx | UART_Mode_Tx;

  HK_EVAL_COMInit(COM1, &UART_InitStructure);
}

/**
  * @brief  Retargets the C library printf function to the UART.
  * @param  None
  * @retval None
  */
int fputc(int ch, FILE *f)
{
  /* Place your implementation of fputc here */

  /* e.g. write a character to the UART */
  UART_SendData(EVAL_COM1, (uint8_t) ch);

  /* Loop until transmit data register is empty */
  while (UART_GetFlagStatus(EVAL_COM1, UART_FLAG_TC) == RESET)
  {
	  
  }

  return ch;
}

/**
  * @brief  Inserts a delay time.
  * @param  nCount: specifies the delay time length.
  * @retval None
  */
static void Delay(__IO uint32_t nCount)
{
  /* Decrement nCount value */
  while (nCount != 0)
  {
    nCount--;
  }
}

#ifdef  USE_FULL_ASSERT

/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t* file, uint32_t line)
{
  /* User can add his own implementation to report the file name and line number,
     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */

  /* Infinite loop */
  while (1)
  {
  }
}
#endif


